package com.example.bookappgraduation;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

import com.example.bookappgraduation.R;


public class StartActivity extends AppCompatActivity {
    TextView textView;
    int index=3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        textView=findViewById(R.id.txt_djs);
        //启动倒计时子线程
        new MyTimer().execute();
    }
    class MyTimer extends AsyncTask<Void,Integer,Void>{
        @Override
        //子线程初始化操作
        protected void onPreExecute() {

            super.onPreExecute();
        }

        @Override
        //子线程运行结束操作
        protected void onPostExecute(Void unused) {

            super.onPostExecute(unused);
        }

        @Override
        //子线程运行过程中的数据交互，一般用来修改主线程uI
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            textView.setText("  "+values[0]);
        }

        @Override
        //子线程中必须要重写的值
        protected Void doInBackground(Void... voids) {
            //耗时操作:等待1秒倒计时数值time-1秒
            while (index>0){
                //把要显示在UI界面的值发送到onProgressUpdate方法中用于显示
                publishProgress(index);
                try {
                    Thread.sleep(1000);
                    index--;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //如果倒计时结束则跳转到登录界面
            if(index<=0) {
                Intent intent = new Intent();
                intent.setClass(StartActivity.this, LoginActivity.class);
                startActivity(intent);
            }
            return null;
        }
    }


}